<!--
# coding=UTF-8
# Copyright (c) 2025 Bird Software Solutions Ltd
# This program and the accompanying materials
# are made available under the terms of the Eclipse Public License 2.0
# which accompanies this distribution, and is available at
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
#    Neil Mackenzie - initial API and implementation
#    Benjamin Arfa - improvements
#
-->
{% extends "base.html" %}

{% block title %}Trail Lineage List{% endblock %}

{% block content %}
<div class="container mt-4">
    <h2>Trail Lineage Viewer</h2>
    <p class="text-muted">Select a trail to visualize its data lineage</p>
    
    {% if trails %}
    <div class="table-responsive">
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>Trail Name</th>
                    <th>Created</th>
                    <th>Execution Context</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                {% for trail in trails %}
                <tr>
                    <td>{{ trail.name }}</td>
                    <td>{{ trail.created_at|date:"Y-m-d H:i:s" }}</td>
                    <td>
                        {% if trail.execution_context %}
                        <code style="font-size: 0.85rem;">{{ trail.execution_context|truncatechars:50 }}</code>
                        {% else %}
                        <span class="text-muted">-</span>
                        {% endif %}
                    </td>
                    <td>
                        <div class="btn-group" role="group">
                            <a href="{% url 'pybirdai:trail_lineage_viewer' trail.id %}" class="btn btn-primary btn-sm">
                                <i class="fas fa-project-diagram"></i> View Lineage
                            </a>
                            <a href="{% url 'pybirdai:trail_filtered_lineage_viewer' trail.id %}" class="btn btn-success btn-sm">
                                <i class="fas fa-filter"></i> View Filtered Lineage
                            </a>
                            <div class="btn-group" role="group">
                                <button type="button" class="btn btn-outline-secondary btn-sm dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
                                    <i class="fas fa-download"></i> JSON
                                </button>
                                <ul class="dropdown-menu">
                                    <li>
                                        <a class="dropdown-item" href="{% url 'pybirdai:get_trail_lineage_summary' trail.id %}" target="_blank">
                                            <i class="fas fa-chart-bar"></i> Summary JSON
                                        </a>
                                    </li>
                                    <li>
                                        <a class="dropdown-item" href="{% url 'pybirdai:get_trail_complete_lineage' trail.id %}" target="_blank">
                                            <i class="fas fa-database"></i> Complete Lineage JSON
                                        </a>
                                    </li>
                                    <li>
                                        <a class="dropdown-item" href="{% url 'pybirdai:get_trail_filtered_lineage' trail.id %}" target="_blank">
                                            <i class="fas fa-filter"></i> Filtered Lineage JSON
                                        </a>
                                    </li>
                                    <li><hr class="dropdown-divider"></li>
                                    <li>
                                        <a class="dropdown-item download-json-btn" href="#" 
                                           data-url="{% url 'pybirdai:get_trail_lineage_summary' trail.id %}" 
                                           data-filename="trail_{{ trail.id }}_summary.json">
                                            <i class="fas fa-download"></i> Download Summary
                                        </a>
                                    </li>
                                    <li>
                                        <a class="dropdown-item download-json-btn" href="#" 
                                           data-url="{% url 'pybirdai:get_trail_complete_lineage' trail.id %}" 
                                           data-filename="trail_{{ trail.id }}_complete.json">
                                            <i class="fas fa-download"></i> Download Complete
                                        </a>
                                    </li>
                                    <li>
                                        <a class="dropdown-item download-json-btn" href="#" 
                                           data-url="{% url 'pybirdai:get_trail_filtered_lineage' trail.id %}" 
                                           data-filename="trail_{{ trail.id }}_filtered.json">
                                            <i class="fas fa-download"></i> Download Filtered
                                        </a>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    {% else %}
    <div class="alert alert-info" role="alert">
        <i class="fas fa-info-circle"></i> No trails found. Execute a datapoint with lineage tracking enabled to create trails.
    </div>
    {% endif %}
</div>
{% endblock %}

{% block extra_js %}
<script>
function downloadTrailJSON(url, filename) {
    fetch(url)
        .then(response => {
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            return response.json();
        })
        .then(data => {
            // Create blob and download
            const blob = new Blob([JSON.stringify(data, null, 2)], {
                type: 'application/json'
            });
            const downloadUrl = window.URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = downloadUrl;
            a.download = filename;
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
            window.URL.revokeObjectURL(downloadUrl);
        })
        .catch(error => {
            console.error('Download error:', error);
            alert('Error downloading JSON: ' + error.message);
        });
}

// Add event listeners for download buttons
document.addEventListener('DOMContentLoaded', function() {
    document.querySelectorAll('.download-json-btn').forEach(function(button) {
        button.addEventListener('click', function(e) {
            e.preventDefault();
            const url = this.dataset.url;
            const filename = this.dataset.filename;
            downloadTrailJSON(url, filename);
        });
    });
});
</script>
{% endblock %}